<?php
/*由bean脚本生成 powerBy--joe*/

namespace app\common\manage;

use app\common\bean\GlobalBean;
use app\common\bean\StoreBean;
use app\common\enum\StoreCashStatusEnum;
use app\common\model\StoreModel;
use app\common\utils\CommonUtil;
use app\common\validate\StoreCashValidate;
use app\common\model\StoreCashModel;
use app\common\bean\StoreCashBean;
use app\common\bean\ListMap;
use app\common\utils\TimeUtil;
use app\common\exception\MyException;
use Exception;

class StoreCashManage extends BaseManage
{
    /**
     * @return StoreCashManage
     */
    static function getInstance()
    {
        return parent::getInstance();
    }
    public function cash(StoreCashBean $storeCashBean){
        $cash_price = $storeCashBean->getParameter('cash_price');
        $storeBean = $this->deduction( $cash_price );

        $fee = $cash_price*$storeBean->getRate()/1000;
        $fee = $fee>0.1 ? $fee : 0.1;
        $data[StoreCashBean::$storeId] = session(GlobalBean::$storeId);
        $data[StoreCashBean::$orderNumber] = CommonUtil::getInstance()->getUnique('cash');
        $data[StoreCashBean::$cashBankName] = $storeCashBean->getParameter('cash_bank_name');
        $data[StoreCashBean::$cashBankNumber] = $storeCashBean->getParameter('cash_bank_number');
        $data[StoreCashBean::$cashPrice] = $cash_price;
        $data[StoreCashBean::$fee] = $fee;
        $data[StoreCashBean::$rate] = $storeBean->getRate();
        $data[StoreCashBean::$createdAt] = date('Y-m-d H:i:s');
        return StoreCashModel::getInstance()->insertGetId( $data );
    }
    protected function deduction($cash_price){
        $storeBean = new StoreBean();
        $storeBean->setId(session(GlobalBean::$storeId));
        $storeBean = StoreManage::getInstance()->getInfo($storeBean);
        if($cash_price>$storeBean->getBlance()){
            MyException::throwMyException("余额不足");
        }
        StoreModel::getInstance()->where([StoreBean::$id=>$storeBean->getId()])->setDec(StoreBean::$blance, $cash_price);
        return $storeBean;
    }

    /**
     * @desc 获取分页数据
     * @param ListMap $listMap
     * @return ListMap $listMap
     * @throws Exception
     **/
    public function getPaginatorList(ListMap $listMap)
    {
        $conditionMap = new ListMap();
        $conditionMap->setData($listMap->getData());
        if ($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap))
            $conditionMap->setParameter(StoreCashBean::$alias . POINST . StoreCashBean::$createdAt, $timeArray);
        $conditionMap->setAlias(StoreCashBean::$alias);
        $listMap->setField(StoreCashBean::$alias . ALL_FIELD);
        !$this->getParamBool($listMap, "orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName') . SPACE . $listMap->getParameter('orderType'));
        if( $listMap->getParameter(GlobalBean::$storeId)==0 ){
            $conditionMap->removeField(StoreCashBean::$storeId);
            $conditionMap->setWith('store');
        }
        $conditionMap->setParameter(StoreCashBean::$status,$listMap->getParameter(StoreCashBean::$status));
        $conditionMap->setOrder(StoreCashBean::$id.' DESC');
        $resultMap = StoreCashModel::getInstance()->getList($conditionMap);
        $resultMap ?: MyException::throwMyException("查询数据失败");
        return $resultMap;
    }

    /**
     * @desc 添加数据
     * @param StoreCashBean $storeCashBean
     * @return int int
     * @throws Exception
     **/
    public function add($storeCashBean)
    {
        StoreCashValidate::getInstance()->goCheck(StoreCashValidate::$add, $storeCashBean->getData());
        // Todo:添加业务处理代码...


        $storeCashBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $result = StoreCashModel::getInstance()->addDataGetInsertId($storeCashBean->getData());
        $result ?: MyException::throwMyException("插入数据失败");
        return $result;
    }

    /**
     * @desc 编辑数据
     * @param StoreCashBean $storeCashBean
     * @return int int
     * @throws Exception
     **/
    public function edit($storeCashBean)
    {
        StoreCashValidate::getInstance()->goCheck(StoreCashValidate::$edit, $storeCashBean->getData());
        // Todo:添加业务处理代码...


        $result = StoreCashModel::getInstance()->updatedData([StoreCashBean::$id => $storeCashBean->getId()], $storeCashBean->getData());
        $result ?: MyException::throwMyException("更新失败");
        return $result;
    }

    /**
     * @desc 删除数据
     * @param StoreCashBean $storeCashBean
     * @return int int
     * @throws Exception
     **/
    public function delete($storeCashBean)
    {
        StoreCashValidate::getInstance()->goCheck(StoreCashValidate::$pkId, $storeCashBean->getData());
        // Todo:添加业务处理代码...


        $result = StoreCashModel::getInstance()->deleteData([StoreCashBean::$id => $storeCashBean->getId()]);
        $result ?: MyException::throwMyException("删除失败");
        return $result;
    }

    /**
     * @desc 根据主建获取数据
     * @param StoreCashBean $storeCashBean
     * @return StoreCashBean $storeCashBean
     * @throws Exception
     **/
    public function getInfo($storeCashBean)
    {
        StoreCashValidate::getInstance()->goCheck(StoreCashValidate::$pkId, $storeCashBean->getData());
        $where = [StoreCashBean::$id => $storeCashBean->getId()];
        !$storeCashBean->getStoreId() ?: $where[StoreCashBean::$storeId] = $storeCashBean->getStoreId();
        $result = StoreCashModel::getInstance()->selectData($where);
        $result ?: MyException::throwMyException("获取信息失败");
        $storeCashBean->setData($result);
        return $storeCashBean;
    }

    /** 审核提现订单
     * @param StoreCashBean $storeCashBean
     * @return bool
     * @throws Exception
     */
    public function reviewStoreCash(StoreCashBean $storeCashBean){
        !$storeCashBean->getStoreId() ?: MyException::throwMyException("非法操作");
        StoreCashValidate::getInstance()->goCheck(StoreCashValidate::$edit, $storeCashBean->getData());
        $storeCashBeanResouce = new StoreCashBean();
        $result = StoreCashModel::getInstance()->selectData([StoreCashBean::$id => $storeCashBean->getId()]);
        $result ?: MyException::throwMyException("获取旧数据出错");
        $storeCashBeanResouce->setData($result);
        $storeCashBeanResouce->getStatus() == StoreCashStatusEnum::WAITTRY ?: MyException::throwMyException("提现非待审核状态");
        $result = true;
        $updateData = $storeCashBean->getData();unset($updateData[$storeCashBean::$storeId]);
        switch ($storeCashBean->getStatus()){
            case StoreCashStatusEnum::TRYSUCCESS://成功
                $result = StoreCashModel::getInstance()->updatedData([StoreCashBean::$id => $storeCashBean->getId()],$updateData);
                break;
            case StoreCashStatusEnum::TRYFAIL://失败
                StoreCashModel::getInstance()->startTrans();
                try{
                    $result = StoreCashModel::getInstance()->updatedData([StoreCashBean::$id => $storeCashBean->getId()],$updateData);
                    $result ?: MyException::throwMyException("更新失败");
                    $result = StoreModel::getInstance()->where(StoreBean::$id,$storeCashBeanResouce->getStoreId())->inc(StoreBean::$blance,$storeCashBeanResouce->getCashPrice() + $storeCashBeanResouce->getFee())->update();
                    $result ?: MyException::throwMyException("更新余额失败");
                    StoreCashModel::getInstance()->commit();
                }catch (Exception $exception){
                    StoreCashModel::getInstance()->rollback();
                    $result = false;
                }
                break;
            default:
                MyException::throwMyException("状态出错");
        }
        $result ? true : MyException::throwMyException("审核失败");
        return true;
    }
}
